﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.IO.Ports;
namespace BWindingMachine
{
    class Data
    {
 
     
        public static SerialPort sp1 = new SerialPort("COM2", 115200, Parity.Even, 8, System.IO.Ports.StopBits.One);
        public static String ss;
        public static String Data_buf = "";
        public static String progrm_name = "";
        public static bool continuity = false;
        public static int error_code_num = 0;
        public static Int64 DataInput_code = 0;
        public static Int64 DataOutput_code = 0;
        public static int run_time = 0;
        public static int stop_time = 0;
        public static int production = 0;
        public static int production_OK = 0;
        public static int all_production_OK = 0;
        public static int error_kaxian_num = 0;
        public static int now_rounds = 0;
        public static int cycle_time = 0;
        public static int Encode1 = 0;
        public static int Encode2 = 0;

        public static bool[] error_code = new bool[32];
        public static int [] DataInput = new int[64];
        public static int [] DataOutput = new int[64];
        public static int [] var = new int[40];
        public static int [] Parameter = new int[40];
        public static int level = 0;//权限级别
        /// <summary>
        /// 机器参数
        /// </summary>
        public static int songxian_acc
        {
            get { return Parameter[0]; }
            set { Parameter[0] = value; }
        }
        public static int songxian_dec
        {
            get { return Parameter[1]; }
            set { Parameter[1] = value; }
        }
        public static int chuansong_acc
        {
            get { return Parameter[2]; }
            set { Parameter[2] = value; }
        }
        public static int chuansong_dec
        {
            get { return Parameter[3]; }
            set { Parameter[3] = value; }
        }
        public static int xuanzhuan_acc
        {
            get { return Parameter[4]; }
            set { Parameter[4] = value; }
        }
        public static int xuanzhuan_dec
        {
            get { return Parameter[5]; }
            set { Parameter[5] = value; }
        }
        public static int raoxian_acc
        {
            get { return Parameter[6]; }
            set { Parameter[6] = value; }
        }
        public static int raoxian_dec
        {
            get { return Parameter[7]; }
            set { Parameter[7] = value; }
        }
        public static int xuanzhuan_1_offset
        {
            get { return Parameter[8]; }
            set { Parameter[8] = value; }
        }
        public static int xuanzhuan_2_offset
        {
            get { return Parameter[9]; }
            set { Parameter[9] = value; }
        }
        public static int xuanzhuan_1_angle
        {
            get { return Parameter[10]; }
            set { Parameter[10] = value; }
        }
        public static int xuanzhuan_2_angle
        {
            get { return Parameter[11]; }
            set { Parameter[11] = value; }
        }

        public static int nuanji_time
        {
            get { return Parameter[12]; }
            set { Parameter[12] = value; }
        }
        public static int bilichi1
        {
            get { return Parameter[13]; }
            set { Parameter[13] = value; }
        }
        public static int bilichi2
        {
            get { return Parameter[14]; }
            set { Parameter[14] = value; }
        }

/// <summary>
/// 参数
/// </summary>
        public static int total_1_length
        {
            get { return var[0]; }
            set { var[0] = value; }
        }
        public static int total_2_length
        {
            get { return var[1]; }
            set { var[1] = value; }
        }
        public static int yuchuxian_1_length
        {
            get { return var[2]; }
            set { var[2] = value; }
        }
        public static int yuchuxian_2_length
        {
            get { return var[3]; }
            set { var[3] = value; }
        }
        public static int slow_length
        {
            get { return var[4]; }
            set { var[4] = value; }
        }
        public static int acc_length
        {
            get { return var[5]; }
            set { var[5] = value; }
        }
        public static int dec_length
        {
            get { return var[6]; }
            set { var[6] = value; }
        }
        public static int songxian_speed
        {
            get { return var[7]; }
            set { var[7] = value; }
        }
        public static int chuansong_speed
        {
            get { return var[8]; }
            set { var[8] = value; }
        }
        public static int xuanzhuan_speed
        {
            get { return var[9]; }
            set { var[9] = value; }
        }

        public static int raoxian_slow_speed
        {
            get { return var[10]; }
            set { var[10] = value; }
        }
        public static int raoxian_high_speed
        {
            get { return var[11]; }
            set { var[11] = value; }
        }
        public static int shangliao_1_jiajin_time
        {
            get { return var[12]; }
            set { var[12] = value; }
        }
        public static int shangliao_1_dakai_time
        {
            get { return var[13]; }
            set { var[13] = value; }
        }
        public static int dingxian_1_daowei_time
        {
            get { return var[14]; }
            set { var[14] = value; }
        }
        public static int qiexian_1_daowei_time
        {
            get { return var[15]; }
            set { var[15] = value; }
        }

        public static int xialiao_1_xiqu_time
        {
            get { return var[16]; }
            set { var[16] = value; }
        }
        public static int chuliao_manliao_time
        {
            get { return var[17]; }
            set { var[17] = value; }
        }
        public static int shangliao_2_jiajin_time
        {
            get { return var[18]; }
            set { var[18] = value;}
        }
        public static int shangliao_2_dakai_time
        {
            get { return var[19]; }
            set { var[19] = value; }
        }
        public static int dingxian_2_daowei_time
        {
            get { return var[20]; }
            set { var[20] = value; }
        }
        public static int qiexian_2_daowei_time
        {
            get { return var[21]; }
            set { var[21] = value; }
        }
        public static int xialiao_2_xiqu_time
        {
            get { return var[22]; }
            set { var[22] = value;}
        }
     



        public static void action()
        {
            try
            {
                if (sp1 == null)
                {
                    sp1 = new SerialPort("COM2", 115200, Parity.Even, 8, System.IO.Ports.StopBits.One);
                }

                sp1.ReceivedBytesThreshold = 1;
                sp1.Open();

                sp1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(OnDataReceived);

            }
            catch
            {


                MessageBox.Show("串口已打开");

            }


        }
        public static void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {

                int n = sp1.BytesToRead;

                byte[] buf = new byte[n];
                sp1.Read(buf, 0, n);
                ss = System.Text.Encoding.Default.GetString(buf, 0, n);
                Data_buf = Data_buf + ss;
                if (ss.Contains("*"))
                {
                    Communication(Data_buf);
                    Data_buf = "";
                }
            }
            catch 
            
            { }
        }
        public static void Communication(string str) 
        {
            try
            {

                string[] Arrary = str.Split('#');
                if (str.Contains("message") && Arrary.Length >= 13) //动作
                {
                    stop_time = Convert.ToInt32(Arrary[1]);
                    run_time = Convert.ToInt32(Arrary[2]);
                    DataOutput_code = Convert.ToInt64(Arrary[3]);
                    DataInput_code = Convert.ToInt64(Arrary[4]);
                    error_code_num = Convert.ToInt32(Arrary[5]);
                    production = Convert.ToInt32(Arrary[6]);
                    production_OK = Convert.ToInt32(Arrary[7]);
                    all_production_OK = Convert.ToInt32(Arrary[8]);
                    error_kaxian_num = Convert.ToInt32(Arrary[9]);
                    cycle_time = Convert.ToInt32(Arrary[10]);
                    now_rounds = Convert.ToInt32(Arrary[11]);
                    Encode1= Convert.ToInt32(Arrary[12]);
                    Encode2 = Convert.ToInt32(Arrary[13]);

                }
             
            }
            catch
            {
            }
         }
        public static void Send(string data)

        {
            try
            {
                byte[] btSend = Encoding.ASCII.GetBytes(data+"*");

                sp1.Write(btSend, 0, btSend.Length);

            }
            catch
            {
                MessageBox.Show("通讯异常！");
            
            }



        }
       
 



    }
}
